package com.example.demo.controller;

import com.example.demo.dto.ProcessNodeDTO;
import com.example.demo.request.BatchCreateProcessNodesRequest;
import com.example.demo.request.CreateProcessNodeRequest;
import com.example.demo.service.ProcessNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/process-configs/{processId}/nodes")
public class ProcessNodeController {

    @Autowired
    private ProcessNodeService processNodeService;

    /**
     * 查询节点列表
     */
    @GetMapping
    public ResponseEntity<List<ProcessNodeDTO>> getProcessNodes(@PathVariable Long processId) {
        List<ProcessNodeDTO> nodes = processNodeService.getProcessNodes(processId);
        return ResponseEntity.ok(nodes);
    }

    /**
     * 创建单个节点
     */
    @PostMapping
    public ResponseEntity<ProcessNodeDTO> createProcessNode(@PathVariable Long processId, @RequestBody CreateProcessNodeRequest request) {
        ProcessNodeDTO nodeDTO = processNodeService.createProcessNode(processId, request);
        return ResponseEntity.ok(nodeDTO);
    }

    /**
     * 批量创建节点
     */
    @PostMapping("/batch")
    public ResponseEntity<List<ProcessNodeDTO>> batchCreateProcessNodes(@PathVariable Long processId, @RequestBody BatchCreateProcessNodesRequest request) {
        List<ProcessNodeDTO> nodes = processNodeService.batchCreateProcessNodes(processId, request.getNodes());
        return ResponseEntity.ok(nodes);
    }

    /**
     * 更新节点
     */
    @PutMapping("/{nodeId}")
    public ResponseEntity<ProcessNodeDTO> updateProcessNode(@PathVariable Long processId, @PathVariable Long nodeId, @RequestBody CreateProcessNodeRequest request) {
        ProcessNodeDTO nodeDTO = processNodeService.updateProcessNode(processId, nodeId, request);
        if (nodeDTO == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(nodeDTO);
    }

    /**
     * 删除节点
     */
    @DeleteMapping("/{nodeId}")
    public ResponseEntity<Void> deleteProcessNode(@PathVariable Long processId, @PathVariable Long nodeId) {
        processNodeService.deleteProcessNode(processId, nodeId);
        return ResponseEntity.noContent().build();
    }
}